﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Smart
{
    public partial class CariBilgi : UserControl
    {
        public CariBilgi()
        {
            InitializeComponent();
            cmbAdresNo.SelectedIndexChanged += new EventHandler(cmbAdresNo_SelectedIndexChanged);
        }

        public void Load(string cariKod)
        {
            using (Query q = new Query(ProgramTipleri.SqlCe))
            {
                q.Add("SELECT        cari_id, cari_adi1, cari_adi2, cari_vade, cari_iskonto_kod, cari_vergi_daire, cari_vergi_no FROM TBL_CARI  WHERE        (cari_kod = @kod)");
                q.Add("@kod", SqlDbType.NVarChar, cariKod, 25);
                var sonuc = q.ToList(new { id = 0, ad1 = "", ad2 = "", vade = 0, iskkod = "", vd = "", vn = "" }).FirstOrDefault();
                if (sonuc != null)
                {
                    txtKod.Text = cariKod;
                    txtAdi.Text = sonuc.ad1 + " " + sonuc.ad2;
                    txtVade.Text = sonuc.vade.ToString();
                    txtVAdi.Text = sonuc.vd;
                    txtVNo.Text = sonuc.vn;
                    txtIsk.Text = sonuc.iskkod;

                    q.Clear();
                    q.Add("SELECT        adres_no, adres, adres_il, adres_ilce,adres_tel1,adres_tel2 FROM            TBL_ADRES WHERE        (adres_cari_id = @id)");
                    q.Add("@id", SqlDbType.Int, sonuc.id);
                    var adr = q.ToList(new { no = "", adres = "", il = "", ilce = "", tel1 = "", tel2 = "" });
                    foreach (var item in adr)
                    {
                        cmbAdresNo.Items.Add(new ComboItem() { Text = item.no, Value = item });
                    }

                    if (cmbAdresNo.Items.Count > 0)
                    {
                        cmbAdresNo.SelectedIndex = 0;
                    }

                    q.Clear();
                    q.Add("SELECT        cari_genel_bakiye, cari_srm_bakiye1, cari_srm_bakiye2, cari_srm_bakiye3, cari_srm_bakiye4, cari_srm_bakiye5, cari_srm_bakiye6, cari_srm_bakiye7,cari_srm_bakiye8, cari_srm_bakiye9, cari_srm_bakiye10 FROM TBL_CARIBAKIYE WHERE        (cari_id = @id)");
                    q.Add("@id", SqlDbType.Int, sonuc.id);
                    var bakiye = q.ToList(new { genel = 0m, bky1 = 0m, bky2 = 0m, bky3 = 0m, bky4 = 0m, bky5 = 0m, bky6 = 0m, bky7 = 0m, bky8 = 0m, bky9 = 0m, bky10 = 0m }).FirstOrDefault();
                    if (bakiye != null)
                    {
                        q.Clear();
                        q.Add("SELECT        srm_adi FROM            TBL_SRM ORDER BY rec_id");
                        var srm = q.ToList(new { srm = "" });

                        DataTable dt = new DataTable();
                        dt.Columns.Add("Cinsi", typeof(string));
                        dt.Columns.Add("Bakiye", typeof(string));
                        List<decimal> bakiyeler = new List<decimal>();
                        bakiyeler.AddRange(new decimal[] { bakiye.bky1, bakiye.bky2, bakiye.bky3, bakiye.bky4, bakiye.bky5, bakiye.bky6, bakiye.bky7, bakiye.bky8, bakiye.bky9, bakiye.bky10 });

                        dt.Rows.Add("Toplam Bakiye", bakiye.genel.ToString("n2"));
                        int i = 0;
                        foreach (var s in srm.Take(10))
                        {
                            if (bakiyeler[i] != 0)
                            {
                                dt.Rows.Add(s.srm, bakiyeler[i].ToString("n2"));
                            }
                            i++;
                        }

                        gridView1.VeriKaynagi = dt;
                        gridView1.AutoSizeCol();
                    }

                }
            }
        }

        void cmbAdresNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAdresNo.SelectedIndex > -1)
            {
                var adres = ((ComboItem)cmbAdresNo.SelectedItem).Value.CastTo(new { no = "", adres = "", il = "", ilce = "", tel1 = "", tel2 = "" });
                if (adres != null)
                {
                    txtAdres.Text = adres.adres + " " + adres.ilce + "/" + adres.ilce;
                    txtTel.Text = adres.tel1 + "-" + adres.tel2;
                }
            }
        }

    }
}
